package com.starsea.book.ui.newpage.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.starsea.book.R
import com.starsea.book.base.VMBaseFragment
import com.starsea.book.databinding.FragmentBookRanksChildBinding
import com.starsea.book.help.config.ReadTipConfig.page
import com.starsea.book.ui.book.info.BookInfoActivity
import com.starsea.book.ui.newpage.adapter.BookRanksAdapter
import com.starsea.book.ui.newpage.viewmodel.HomeViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-07-12 19 GMT+8
 * @desc :book Depository
 */
class BookRanksChildFragment :
    VMBaseFragment<HomeViewModel>(R.layout.fragment_book_ranks_child) {

    val bundle = Bundle()

    //    bundle.putString("typeId", value)
//    bundle.putString("rankType", toolsList[arg0].value)
    var typeId = ""
    var rankType = ""

    var page = 1
    private val binding by viewBinding(FragmentBookRanksChildBinding::bind)
    override val viewModel by viewModels<HomeViewModel>()
    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
        mAdapter = BookRanksAdapter(requireContext())
        binding.rvContent.layoutManager = LinearLayoutManager(requireContext())
        binding.rvContent.adapter = mAdapter

        typeId = arguments?.getString("typeId") ?: ""
        rankType = arguments?.getString("rankType") ?: ""

        LogUtils.e(">>>>>  typeId = $typeId")
        LogUtils.e(">>>>>  rankType = $rankType")

        viewModel.getBookRanksListData(rankType, typeId)
        binding.srlRefresh.setEnableRefresh(false)
        binding.srlRefresh.setEnableLoadMore(false)
//        binding.srlRefresh.setOnRefreshListener {
//            page = 1
//            viewModel.getBookRanksListData(rankType, typeId)
//        }
//        binding.srlRefresh.setOnLoadMoreListener {
//            page++
//            viewModel.getBookRanksListData(rankType, typeId)
//        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity(
                BookInfoActivity::class,
                "bookId" to mAdapter.items[position].bookId!!
            )
        }

        viewModel.bookRanksListResutl.observe(this) {
            when (it.status) {
                200 -> {
                    if (page == 1) {
                        binding.srlRefresh.finishRefresh()
                        mAdapter.submitList(it.data ?: arrayListOf())
                    } else {
                        binding.srlRefresh.finishLoadMore()
                        mAdapter.addAll(it.data ?: arrayListOf())
                    }

                }

                else -> {

                }
            }
        }


    }

    lateinit var mAdapter: BookRanksAdapter

    companion object {
        fun newInstance(): BookRanksChildFragment {
            val args = Bundle()
            val fragment = BookRanksChildFragment()
            fragment.arguments = args
            return fragment
        }
    }


}